<%@page import="com.mes.entity.tdd.Staff"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/pages/hy/common.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>作业信息</title>
<link rel="stylesheet" href="${pageContext.request.contextPath }/css/toastr.min.css">
<script src="${pageContext.request.contextPath }/js/toastr.min.js"></script>
<script type="text/javascript">
    toastr.options.positionClass = 'toast-top-center';
 </script>
<% 
	String name = "";
	Staff staff = (Staff)session.getAttribute("staff");
	name = staff.getStaffname();
%>
<script type="text/javascript">
$(function () {
	var realNuma = 0;
	var ngooda = 0
    //1.初始化Table
    var oTable = new TableInit();
    oTable.Init();

    //2.初始化Button的点击事件
    var oButtonInit = new ButtonInit();
    oButtonInit.Init();
});
var TableInit = function () {
    var oTableInit = new Object();
    //初始化Table
    oTableInit.Init = function () {
       
        $('#tb_departments').bootstrapTable({
                   //请求后台的URL（*）
            method: 'post',                      //请求方式（*）
            striped: true,                      //是否显示行间隔色
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            sortable: false,                     //是否启用排序
            sortOrder: "asc",                   //排序方式
            queryParams: oTableInit.queryParams,//传递参数（*）
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber:1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10,20,30],         //可供选择的每页的行数（*）
            clickToSelect: true,                //是否启用点击选中行
            height: 500,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            uniqueId: "ID",                     //每一行的唯一标识，一般为主键列
            detailView: false,                   //是否显示父子表
            columns: [{
                checkbox: true
            }, {
                field: 'id',
                title: 'ID',
                visible:false
            }, {
                field: 'remark1',
                title: '排产单编号'
            },{
                field: 'pnum',
                title: '品编'
            }, {
                field: 'pname',
                title: '品名',
            },{
                field: 'plannum',
                title: '计划量',
            },{
                field: 'stattime',
                title: '开始时间',
            }]
        });
    }
    
    //得到查询的参数
    oTableInit.queryParams = function (params) {
        var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            limit: params.limit,   //页面大小
            offset: params.offset,  //页码
            machin : $("#deviceId").val(),
        };
        return temp;
    };
    return oTableInit;
};

var ButtonInit = function () {
    var oInit = new Object();
    var postdata = {};

    oInit.Init = function () {
        //初始化页面上面的按钮事件
    };

    return oInit;
};
</script>
<script type="text/javascript">
//页面实时获取时间
		function getCurDate()
		{
		var d = new Date();
		var week;
		switch (d.getDay()){
		case 1: week="星期一"; break;
		case 2: week="星期二"; break;
		case 3: week="星期三"; break;
		case 4: week="星期四"; break;
		case 5: week="星期五"; break;
		case 6: week="星期六"; break;
		default: week="星期天";
		}
		var years = d.getFullYear();
		var month = add_zero(d.getMonth()+1);
		var days = add_zero(d.getDate());
		var hours = add_zero(d.getHours());
		var minutes = add_zero(d.getMinutes());
		var seconds=add_zero(d.getSeconds());
		var ndate = years+"年"+month+"月"+days+"日 "+hours+":"+minutes+":"+seconds+" "+week;
		var divT=document.getElementById("nowDate");
		divT.innerHTML= ndate;
		}
		function add_zero(temp)
		{
		if(temp<10) return "0"+temp;
		else return temp;
		}
		setInterval("getCurDate()",100);
</script>
<script type="text/javascript">
$(function () {
	var number = null;
	//查询车间信息
    $.ajax({
        url:'${pageContext.request.contextPath }/jy/selectCarroom.do',
        type:"POST",
        dataType:'json',
        success:function (result) {
        	 $("#carroom").append("<option>--请选择--</option>");
            for (var i = 0; i < result.length; i++) {
                $("#carroom").append("<option value="+result[i].id+">"+result[i].name+"</option>");
            }
        }
    });
	
	//清除弹窗原数据
	$("#myModal2").on("hidden.bs.modal", function() {
	    $(this).removeData("bs.modal");
	});
	
    //打开 不良登记
    $("#btn").click(function(){
    	$("#addForm")[0].reset();//打开模态框前进行重置
    	
    	$("#remartk2").focus(); 
		$("#remartk2").select(); 
    	
    	$("#btn").attr("data-toggle","modal");
    	$("#btn").attr("data-target","#myModal2");
    });
    
});

//保存不良登记
function ngSave() {
	//获取不良登记的班组
	var team = $('input[name="team"]:checked ').val();
	var defectivetype1 = $('input[name="defectivetype1"]:checked ').val();
	var badproject = $('input[name="badproject"]:checked ').val();
	var dev = $("#deviceId").val();
	var number = $("#number").text();
	var remartk2 = $("#remartk2").val();
	var remartk3 = $("#remartk3").val();
	var pdid = $("#pdid").val();
	if(defectivetype1 == null){
		toastr.warning("请先选择不良类型");
	}else if(team == "" || team == null  || team == undefined) {
		toastr.warning("请选择班组");
	}else {
		$.ajax({
			type:"post",
			url: '${pageContext.request.contextPath }/homeworkInfo/addNoGood.do',
			data: {defectivetype1:defectivetype1,badproject:badproject,team:team,remartk:dev,remartk1: number,remartk2: remartk2,remartk3: remartk3,remartk4: pdid},
			success: function(data){
				if(data){
					toastr.success("添加成功");
					selectNogood();
					createWork();
					$("#addForm")[0].reset();//进行重置
					$("#icode").focus(); 
					$("#icode").select(); 
				}else {
					toastr.error("添加失败");
				}
			}
		});
	}
}


//查询设备信息，根据车间信息
function selectEquipment() {
	var cid = $("#carroom").val();
	$("#deviceId").text("");
    $.ajax({
        url:'${pageContext.request.contextPath }/jy/selectEquipment.do',
        type:"POST",
        dataType:'json',
        data:{cid:cid},
        success:function (result) {
            for (var i = 0; i < result.length; i++) {
                $("#deviceId").append("<option value="+result[i].eid+">"+result[i].name+"</option>");
            }
        }
    });
    
    $("#shedu").text("");
	
	$("#number").text("");
	$("#pname").text("");
	$("#virNum").text("");
	
	//将排产详情和计划详情暂存到页面，保存作业信息时要用
	$("#pcid").val(""); 
	$("#pdid").val(""); 
	
	document.getElementById("mtype1").innerHTML = ""; 
	document.getElementById("mname1").innerHTML = "" ;
	$("#realNum").text("");
	$("#nogood").text("");
	$("#good").text(parseInt(realNum) - parseInt(""));
    
}

/* 打开排产单列表的模态框 */
function pcdxz() {
	var equipment = $.trim($("#deviceId").val());
	 if(equipment == "" || equipment == null  || equipment == undefined || equipment == 0 ) {
			toastr.warning("请先选择设备");  
		 return;
	 }else {
		 $("#addForm")[0].reset();//打开模态框前进行重置
			$("#addModal").modal(open);
		 $('#tb_departments').bootstrapTable(  
             "refresh",  
             {  
            	url: '${pageContext.request.contextPath }/jy/selectDysheduDetailByAllw.do?machin='+equipment 
             }  
    	  ); 
	 }
}

/* 选择排产单详情后，将数据带回到页面 */
function submitpc() {
	var a= $('#tb_departments').bootstrapTable('getSelections');  
	if(a.length  == 1) {
		//只生产一条
		
		$("#shedu").text(a[0].remark1);
		
		$("#number").text(a[0].pnum);
		$("#pname").text(a[0].pname);
		$("#virNum").text(a[0].plannum);
		
		//将排产详情和计划详情暂存到页面，保存作业信息时要用
		$("#pcid").val(a[0].remark5); 
		$("#pdid").val(a[0].id); 
		
		//根据产品编号查询BOM表，带回：物料编号，物料名称
		$.ajax({
			type:"post",
			url: '${pageContext.request.contextPath }/allw/selectBom.do',
			data: {pnum:a[0].pnum},
			success: function(data){
				var jsonObject = eval("(" + data + ")");
				document.getElementById("mtype1").innerHTML = jsonObject.materialmodel; 
				document.getElementById("mname1").innerHTML = jsonObject.name ;
			}
		});
		
		selectAct();
		$("#addModal").modal('hide');
	}else if(a.length > 1) {
		toastr.warning("对不起，只能选择一条");  
	}
}
	
/* 查询实际量 */
function selectAct() {
	var pdid = $("#pdid").val();
	//根据排产单详情id统计
	$.ajax({
		type:"post",
		async:false, 
		url: '${pageContext.request.contextPath }/code/selectAct.do',
		data: {did:pdid},
		success: function(data){
			$("#realNum").text(data);
			selectNogood();
		}
	});
}
/* 查询不良品数量 */
function selectNogood() {
	var pdid = $("#pdid").val();
	var realNum = $("#realNum").text();
	//根据排产单详情id统计
	$.ajax({
		type:"post",
		async:false, 
		url: '${pageContext.request.contextPath }/code/selectNogood.do',
		data: {did:pdid},
		success: function(data){
			$("#nogood").text(data);
			$("#good").text(parseInt(realNum) - parseInt(data));
		}
	});
}

/*不良登记 - 条码校验 */
function checkNogood() {
	var code = $(remartk2).val();
	//校验条码
	$.ajax({
		type:"post",
		async:false, 
		url: '${pageContext.request.contextPath }/code/checkNogoodCode.do',
		data: {code:code},
		success: function(data){
			if(data >0) {
				 toastr.warning("对不起，此产品已做不良登记！");
			}else if(data < 0) {
				 toastr.warning("对不起，产品中找不到该条码！");
			}
		}
	});
}


/**注销*/
function logout() {
	overWork();
}

/**结束作业*/
function overWork() {
	//作业人员
	var woker1 = $("#name").text();
	var virNum = $("#virNum").text();
	var realNum = $("#realNum").text();
	
	if((woker1 == "" || woker1 == null)){
		//toastr.info("作业未正常开始");
		window.location.href = "${pageContext.request.contextPath }/toLogin.do";
	}else {
		//要获取排产详情id
		var did = $("#pdid").val();
		var pid = $("#pcid").val(); 
		$.ajax({
			type:"post",
			url: '${pageContext.request.contextPath }/allw/closeWork.do',
			data: {pnum:virNum,snum:realNum,did:did,pid:pid},
			success: function(data){
				if(data >0) {
					window.location.href = "${pageContext.request.contextPath }/toLogin.do";
				}
			}
		});
	}
}
	
/* 填加作业信息 */
function createWork() {
	//计划量
	 var pnum = document.getElementById("virNum").innerHTML;
	//实际量
	var snum = document.getElementById("realNum").innerHTML;
	//不良品
	var ngood = document.getElementById("nogood").innerHTML;
	//良品
	var good = document.getElementById("good").innerHTML;
	//品编
	var nmum = document.getElementById("number").innerHTML;
	//产品名称
	var pname = document.getElementById("pname").innerHTML;
	
	//作业人员
		var woker1 = $("#name").text();
	//设备
	var machin = $.trim($("#deviceId").val());
	
	var pid =  $("#pcid").val(); 
	var did = $("#pdid").val(); 
	
	$.ajax({
		type:"post",
		url: '${pageContext.request.contextPath }/allw/createWork.do',
		data: {pnum:pnum,snum:snum,ngood:ngood,good:good,nmum:nmum,pname:pname
			,woker1:woker1,machin:machin,pid:pid,did:did},
		success: function(data){
			if(data == 1){
			}else {
			}
		}
	});
}
	
	
//input 开始数字
var inpK = 1;
//input 结束数字
var inpJ = 10;

function keyDown(e){  
    //IE内核浏览器  
     if (navigator.appName == 'Microsoft Internet Explorer'){  
           var keycode = event.keyCode;  
           var realkey = String.fromCharCode(event.keyCode);  
     }else {//非IE内核浏览器  
           var keycode = e.which;  
           var realkey = String.fromCharCode(e.which);  
     }  

     //监听Ctrl键  
     if(keycode==13){ 
    	 
  	   if($("#remartk2").val() != null && $("#remartk2").val() != "" && $("#remartk2").val() != undefined) {
  		 return;
  	   }else {
  		 if($("#shedu").text() != null && $("#shedu").text() != "" && $("#shedu").text() != undefined) {
	  		 if($("#icode").val() != null && $("#icode").val() != "" && $("#icode").val() != undefined) {
	   			getId();
	   	   	 }
	  	   }else {
	  		 toastr.warning("对不起，请先选择排产单！");
	  		   return;
	  	   }
  	   }
    }
 }  
//按键事件
document.onkeydown=keyDown;

function getId(){ 
	var code=$.trim($("#icode").val());
	var mid = inpK;
		//alert(mid+"------");
	/*  if(code.length != 16 || code.substring(1,3) != "PS"){
		toastr.warning("请扫码正确的膜管编号！");
		mid = parseInt(mid) - parseInt(1);
        $("#icode").select();
		return;
	 }else {
		 
	 } */
	 
	 $.ajax({
    	type:'post',
    	async:false, 
    	url:'${pageContext.request.contextPath }/code/selectCode.do',
    	data: {"code":code},
    	success:function(data){
    		if(data>0){
    			toastr.warning("对不起，数据已存在，请重新扫码！");
    			mid = parseInt(mid) - parseInt(1);
    	        $("#icode").select();
    			return;
    		}else{
    			
    			for (var i = 1; i<mid; i++) {
    				//页面已存在的每一个膜管的码
    				var stube = document.getElementById("c"+i).innerHTML;
    				//与c1中的码做比较
    				if(code == stube ){
    					toastr.warning("对不起，当前页面已有，请重新扫码！");
    					mid = parseInt(mid) - parseInt(1);
    					$("#icode").select();
    					return;
    				}
    			}
    			
    			if(parseInt(inpK) < parseInt(inpJ)+1){
    				var pdid = $("#pdid").val();
    				var pcid = $("#pcid").val();
    				var shedu = $("#shedu").text();
    				 $.ajax({
    					type: "post",
    					url: '${pageContext.request.contextPath }/code/insert.do',
    					data: {code:code,did:pdid,pid:pcid,remark:0,remark2:shedu},
    					success: function(data){
    						if(data >0) {
    							selectAct();
    							createWork();
    							selectNogood();
    							$("#c"+inpK).text(code);
    							inpK = inpK+1;
    							$("#icode").focus(); 
    							$("#icode").select(); 
    						}else {
    							toastr.warning("对不起，保存失败！");
    						}
    					}
    				});
    			}else{
    				var pdid = $("#pdid").val();
    				var pcid = $("#pcid").val();
    				var shedu = $("#shedu").text();
    				var set = $("#set").text();
    				 $.ajax({
    					type: "post",
    					url: '${pageContext.request.contextPath }/code/insert.do',
    					data: {code:code,did:pdid,pid:pcid,remark:0,remark1:set,remark2:shedu},
    					success: function(data){
    						if(data >0) {
    							selectAct();
    							createWork();
    							selectNogood();
    							$("#c1").text(code);
    							$("#now").text(1);
    							$("#icode").focus(); 
    							$("#icode").select(); 
    						}else {
    							toastr.warning("对不起，保存失败！");
    						}
    					}
    				 });
    				//清空table
    				for (var i = 2; i < 13; i++) {
    					$("#c"+i).text("");
    				}
    				inpK = 1;
    				//input 结束数字
    				inpJ = 10;
    				//input 开始id
    				$("#icode").focus(); 
    				$("#icode").select();  
    			} //表格循环结束
    		}
    	}
    });
}

</script>
<style type="text/css">
	body {
		background-color:#F5F5F5;
		font-family:Microsoft YaHei;
		font-size: 28px;
	}
	table tr td {
		background-color:#F5F5F5;
	}
	
	#mytable tr td {
		background-color:#FFFFFF;
	}
</style>
</head>
<body>
	<table class="table table-bordered">
		<tbody align="center" valign="middle">
			<tr>
				<td rowspan="2" width="30%" style="background-color: #337AB7; vertical-align: middle;">
					<img alt="" src="${pageContext.request.contextPath}/images/logo3.png" width="400px" height="100px;">
				</td>
				<td rowspan="2" width="25%" style="background-color: #337AB7;font-size: 48px;color: #ffffff; vertical-align: middle;">按个扫码</td>
				<td width="20%" style="background-color: #337AB7;color: #ffffff; font-size: 36px; vertical-align: middle;">作业员</td>
				<td rowspan="2" style="background-color: #337AB7;color: #ffffff; font-size: 36px; vertical-align: middle;" id="nowDate"></td>
			</tr>
			<tr>
				<td style="background-color: #337AB7; font-size: 36px;color: #ffffff" id="name"><%=name%></td>
			</tr>
			<tr>
				<td colspan="2">
					<table class="table table-bordered" style="line-height: 100px;">
						<tbody align="center">
						<tr>
							<td>车间</td>
							<td>
								<select id="carroom" onchange="selectEquipment()" style="width: 200px;">
								</select>
							</td>
							<td>设备</td>
							<td>
								<select id="deviceId"  style="width: 200px;">
								</select>
							</td>
						</tr>
						<tr>
							<td>
								<button onclick="pcdxz()" class="btn btn-primary btn-lg">选择排产单</button>
								<input type="hidden" id="pcid">
								<input type="hidden" id="pdid">
							</td>
							<td colspan="3" id="shedu"></td>
						</tr>
						<tr>
							<td>品编</td>
							<td colspan="3" id="number"></td>
						</tr>
						<tr>
							<td>品名</td>
							<td colspan="3" id="pname"></td>
						</tr>
						<tr>
							<td>物料型号</td>
							<td colspan="3" id="mtype1"></td>
						</tr>
						<tr>
							<td>物料名称</td>
							<td colspan="3" id="mname1"></td>
						</tr>
						<tr>
							<td>计划量</td>
							<td id="virNum"></td>
							<td>实际量</td>
							<td id="realNum"></td>
						</tr>
						<tr>
							<td>良品</td>
							<td id="good"></td>
							<td>不良品</td>
							<td id="nogood"></td>
						</tr>
						</tbody>
					</table>
				</td>
				
				<td colspan="2">
					<table class="table table-bordered">
						<tbody align="center">
						<tr>
							<td>请扫描产品条码</td>
						</tr>
						<tr>
							<td><input type="text" id="icode" placeholder="扫码输入框" style="width: 99%;border:1px #FFFFFF solid;text-align: center;"></td>
						</tr>
						</tbody>
					</table>
					<table class="table table-bordered">
						<tbody align="center">
						<tr>
							<td width="1%">1</td>
							<td id="c1" width="3%"></td>
							<td width="1%">6</td>
							<td id="c6" width="7%"></td>
						</tr>
						<tr>
							<td>2</td>
							<td id="c2"></td>
							<td>7</td>
							<td id="c7"></td>
						</tr>
						<tr>
							<td>3</td>
							<td id="c3"></td>
							<td>8</td>
							<td id="c8"></td>
						</tr>
						<tr>
							<td>4</td>
							<td id="c4"></td>
							<td>9</td>
							<td id="c9"></td>
						</tr>
						<tr>
							<td>5</td>
							<td id="c5"></td>
							<td>10</td>
							<td id="c10"></td>
						</tr>
						</tbody>
					</table>
					<button id="btn" class="btn btn-primary  btn-lg btn-block">不良品登记</button>
					<button id="btn1" onclick="logout()" class="btn btn-danger  btn-lg btn-block">注销</button>
				</td>
			</tr>
		</tbody>
	</table>
	
	
	<!-- 选择排产单 -->
	<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content"  style="width: 1000px;margin-left: -25%;">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close" aria-hidden="true" onclick="resets()">&times;</button>
					<h4 class="modal-title" id="addModalLabel">排产单选择</h4>
				</div>
				<table id="tb_departments" style="font-size: 24px;"></table>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="submit" class="btn btn-primary"  onclick = "submitpc()">保存</button>
				</div>
			</div>
		</div>
	</div>
	
	<!-- 不良品登记-->
	<div class="modal fade " id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModal2">
		<div class="modal-dialog" role="document">
			<div class="modal-content" style="width: 150%;margin-left: -25%;">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close" aria-hidden="true" onclick="resets()">&times;</button>
					<h4 class="modal-title" id="addModalLabel">不良品登记</h4>
				</div>
				<form action="" id="addForm">
					<table id="mytable" class="table" style="font-size: 24px;">
						<tbody align="center">
							<tr>
								<td width="30%">产品条码：</td>
								<td><input type="text" id="remartk2" name="remartk2"  style="width: 65%;" onblur="checkNogood()"></td>
							</tr>
							<tr>
								<td>不良数量：</td>
								<td>
									<input type="text" id="remartk3" name="remartk3" style="width: 65%;">
								</td>
							</tr>
							<tr>
								<td>不良类型：</td>
								<td>	
									<input type="radio" name="defectivetype1" value="未成形"/> <font>未成形</font> &nbsp;&nbsp;
									<input type="radio" name="defectivetype1" value="毛边"/> <font>毛边</font> &nbsp;&nbsp;
									<input type="radio" name="defectivetype1" value="冷料"/> <font>冷料</font> &nbsp;&nbsp;
									<input type="radio" name="defectivetype1" value="亮印"/> <font>亮印</font>
								</td>
							</tr>
							<tr>
								<td>不良工程：</td>
								<td>
									<input type="radio" name="badproject"  value="注塑"/> <font>注塑</font> &nbsp;&nbsp;
									<input type="radio" name="badproject"  value="组装"/> <font>组装</font>
								</td>
							</tr>
							<tr>
								<td>作业班组：</td>
								<td>
									<input type="radio" name="team" id="day" value="1"> <font>白班</font> &nbsp;&nbsp;
									<input type="radio" name="team" id="night" value="2"> <font>晚班</font>
								</td>
							</tr>
						</tbody>
					</table>
				</form>
				
				<div class="modal-footer">
					<!-- <span id="returnMessage" class="glyphicon"> </span> -->
					<button id="submitBtn1" onclick="ngSave()" type="button" class="btn btn-primary" style="font-size: 26px" aria-hidden="true">保存</button>
					<button type="button" class="btn btn-default right" data-dismiss="modal"style="font-size: 26px">取消</button>
				</div> 
			</div>
		</div>
	</div>
</body>
</html>